package net.zjitc.arrays;

import java.util.Arrays;

public class TripleTest {
    public static void main(String[] args) {
        final int ROW =5;
        final int COL =5;
        final int NOTZERO = 6;
        int[][] sparse = new int[ROW][COL];
        //构造5*5稀疏矩阵，随机产生非零值，
        for (int i = 0; i < NOTZERO; ) {
            int tempRow =   (int)(Math.random()*100)%ROW; //随机行
            int tempCol =   (int)(Math.random()*100)% COL;//随机列
            if (sparse[tempRow][tempCol] == 0){ //避免二次赋值
                int tempVal = (int)(Math.random()*100);
                sparse[tempRow][tempCol] = tempVal;
                i++;
            }
        }


        for (int i = 0; i <sparse.length ; i++) {
            System.out.println(Arrays.toString(sparse[i]));
        }

        //矩阵压缩
        System.out.println("压缩后的矩阵");
        Triple[] compress = new Triple[NOTZERO+1];//arraylist=1:是否有序--》
        compress[0] = new Triple(ROW,COL,NOTZERO);
        int temp =1;
        for (int i = 0; i < sparse.length; i++) {
            for (int j = 0; j <sparse[i].length ; j++) {
                if (sparse[i][j] != 0){
                    compress[temp] = new Triple(i,j,sparse[i][j]);
                    temp++;
                }
            }
        }

        for (int i = 0; i <compress.length ; i++) {
            System.out.println(compress[i]);
        }
    }
}
